/**
 * link list 
 */
#include <stdio.h>
#include <stdlib.h>

//#ifdef _List_H

#define ELEMENT_TYPE char

struct node;
typedef struct node *ptr_to_node;
typedef ptr_to_node list;
typedef ptr_to_node position;

//prototypes
//{{{
list make_empty( list l );
int is_empty( list l );
int is_last( list l , position p );
position find( ELEMENT_TYPE x , list l );
position find_prev( ELEMENT_TYPE x , list l );
void insert( ELEMENT_TYPE x , list l , position p );
void delete( ELEMENT_TYPE x , list l );

void delete_list( list l );
position header( list l );
position first( list l );
position advance( position p );
ELEMENT_TYPE retrive( position p );

struct node {
    ELEMENT_TYPE element;
    position next;
};
//}}}

//#endif

int 
is_empty( list l ) {
//{{{
    return l->next == NULL;
}//}}}

int 
is_last( position p , list l ) {
//{{{
    return p->next == NULL;
}//}}}

position 
find( ELEMENT_TYPE x , list l ) {
//{{{
    position p;

    p = l->next;
    while( p != NULL && p->element != x ) {
        p = p->next;
    }

    return p;
}//}}}

void
delete( ELEMENT_TYPE x , list l ) {
//{{{
    position p, tem_cell;

    p = find_prev( x, l );
    if( !is_last( p , l ) ) {
        tem_cell = p->next;
        p->next = tem_cell->next;
        free( tem_cell );
    }
}//}}}

position
find_prev( ELEMENT_TYPE x, list l ) {
    position p;

    p = l;
    while( p->next != NULL && p->next->element ) {
        p = p->next;
    }

    return p;
}

void
insert( ELEMENT_TYPE x, list l, position p ) {
    position tem_cell;
    
    tem_cell = malloc( sizeof( struct node ) );
    if( tem_cell == NULL ) {
        printf( "out of space\n" );
        return;
    }

    tem_cell->element = x;
    tem_cell->next = p->next;
    p->next = tem_cell;
}

int
main() {
//{{{
    list bible;
    insert( 'x' , bible , 0 );
    //printf( "%c\n" , bible->next->element );
    return 0;
}//}}}
